package com.indusfo.spc.reportForms.service.impl;

import com.indusfo.spc.reportForms.mapper.WorkerMapper;
import com.indusfo.spc.reportForms.pojo.Worker;
import com.indusfo.spc.reportForms.pojo.WorkerDto;
import com.indusfo.spc.reportForms.service.WorkerService;
import com.indusfo.spc.untlis.DateUtils;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName WorkerServiceImpl
 * @Description 作业人员service
 * @Author louk
 * @Date 2020/2/5 23:04
 */
@Service
public class WorkerServiceImpl implements WorkerService {
    @Resource
    WorkerMapper workerMapper;
    @Override
    public JSONObject queryWorkerInfo(Worker worker) {
        String checkParam = checkParam(worker);
        if(!StringUtils.isEmpty(checkParam)){
            return JSONObject.build(JSONObject.ERROR,checkParam);
        }
        List<WorkerDto> workerDtoList = workerMapper.queryWorkerInfo(worker);
        if(!workerDtoList.isEmpty()){
            int countWorkerInfo = workerMapper.countWorkerInfo(worker);
            return JSONObject.oK("查询成功",workerDtoList,countWorkerInfo);
        }
        return JSONObject.oK("暂无相关数据",workerDtoList,null);
    }
    public String checkParam(Worker worker){
        Integer userId = worker.getUserId();
        String startDate = worker.getStartDate();
        String endDate = worker.getEndDate();
        Integer pagesize = worker.getPagesize();
        Integer pageindex = worker.getPageindex();
        if(pagesize!=null && !"".equals(pagesize) && pageindex!=null && !"".equals(pageindex)){
            worker.setIncept(pagesize * (pageindex - 1));
        }
        /*if("".equals(userId) || userId==null || new Integer(0).equals(userId)){
            return "作业人员不能为空";
        }*/
        if((StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) || (!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate))){
            return "开始时间结束时间必须同时存在";
        }
        if(!StringUtils.isEmpty(startDate)){
            boolean validDate = DateUtils.isValidDate(startDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            if(!validDate){
                return "开始时间格式不正确";
            }
        }else{
            worker.setStartDate(DateUtils.dateSplicer("08:00:00"));
        }
        if(!StringUtils.isEmpty(endDate)){
            boolean validDate = DateUtils.isValidDate(endDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            if(!validDate){
                return "结束时间格式不正确";
            }
        }else{
            worker.setEndDate(DateUtils.dateSplicer(DateUtils.dateByDay(1),"08:00:00"));
        }
        return null;
    }
}
